package com.masterclient.app.businessmodule.settingmodule.crossborder.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.common.bean.base.CommonBaseBean;
import com.masterclient.app.businessmodule.settingmodule.api.SettingHttpService;
import com.masterclient.app.businessmodule.settingmodule.auth.bean.CustomerOcrBean;
import com.masterclient.app.businessmodule.settingmodule.crossborder.model.CrossBorderListBean;
import com.masterclient.app.businessmodule.settingmodule.crossborder.model.DefaultCrossBorderBean;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


public class ManageCrossBorderPresenter extends ManageCrossBorderContract.Presenter{

    @Override
    public void customer_cross_border_list(String page) {
        mView.baseShowPageLoading(false);
        Observable<CrossBorderListBean> observable = SettingHttpService.getInstance().customer_cross_border_list();
        Disposable disposable = toSubscribe(observable, new Consumer<CrossBorderListBean>() {
            @Override
            public void accept(@NonNull CrossBorderListBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                if(bean!=null){
                    if(bean.getData()!=null){
                        mView.customer_cross_border_list_success(bean);
                    }else{
                        mView.customer_cross_border_list_failed(bean.getMsg());
                    }
                } else {
                    mView.customer_cross_border_list_failed("获取列表失败");
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void customer_cross_border_del(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customs_clearance_id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseBean> observable = SettingHttpService.getInstance().customer_cross_border_del(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showDeleteSuccess();
                }else {
                    mView.showDeleteFailed();
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void set_cross_border_default(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customs_clearance_id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<DefaultCrossBorderBean> observable = SettingHttpService.getInstance().setCrossBorderDefault(body);
        Disposable disposable = toSubscribe(observable, (Consumer<DefaultCrossBorderBean>) bean -> {
            if("0".equals(bean.getCode())){
                mView.changeDefaultSuccess();
            }else {
                mView.changeDefaultfailed();
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void customs_ocr(File image, String type) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        RequestBody imageBody = RequestBody.create(MediaType.parse("image/jpeg"), image);
        builder.addFormDataPart("image", "123.jpeg", imageBody);//imgfile 后台接收图片流的参数名
        builder.addFormDataPart("type",type);

        Observable<CommonBaseResponse<CustomerOcrBean>> observable = SettingHttpService.getInstance().customs_ocr(builder.build().parts());
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseResponse<CustomerOcrBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<CustomerOcrBean> bean) throws Exception {
                if(0 == bean.getCode()){
                    mView.OnNewOcrResult(bean.getData());
                }else{
                    mView.OnNewOcrResultFail(bean.getMsg());
                }
            }
        },  new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.OnNewOcrResultFail(o.getMessage());
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void cross_border_manage(String id, String is_default, String real_name, String identity_card, String frontInfo, String backInfo) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customs_clearance_id",id);
            jsonObject.put("is_default",is_default);
            jsonObject.put("real_name",real_name);
            jsonObject.put("identity_card_number",identity_card);
            jsonObject.put("front_info",frontInfo);
            jsonObject.put("back_info",backInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseBean> observable = SettingHttpService.getInstance().cross_border_manage(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseBean>) bean -> {
            if("0".equals(bean.getCode())){
                mView.cross_border_manage_result("1",bean.getMsg());
            }else{
                mView.cross_border_manage_result("",bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.cross_border_manage_result("","网络异常，请重试");
        });
        mRxManager.add(disposable);
    }
}